package com.edu.edu.day45_mvp.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.edu.edu.day45_mvp.R;
import com.edu.edu.day45_mvp.presenter.LoginPresenter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,ILoginView{

    private EditText etPassWord;
    private EditText etUserName;
    private ProgressBar mProgressBar;
    private LoginPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //获得主持人的一个实例
        mPresenter = new LoginPresenter(this);
    }

    private void initView() {
         etUserName = (EditText) findViewById(R.id.et_userName);
         etPassWord = (EditText) findViewById(R.id.et_PassWord);
         findViewById(R.id.bt_login).setOnClickListener(this);
         findViewById(R.id.bt_clear).setOnClickListener(this);
         mProgressBar = (ProgressBar) findViewById(R.id.progress);
    }

    //按钮点击事件
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_login://登录
                mPresenter.login();
                break;
            case R.id.bt_clear://清除
                mPresenter.clear();
                break;
        }
    }

    @Override
    public String getUserName() {
        return etUserName.getText().toString().trim();
    }

    @Override
    public String getPassWord() {
        return etPassWord.getText().toString().trim();
    }

    @Override
    public void showProgressBar() {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgressBar() {
        mProgressBar.setVisibility(View.GONE);
    }

    @Override
    public void loginSucceedActivity() {
        Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
        startActivity(new Intent(this,SecondActivity.class));
    }

    @Override
    public void loginFialedView() {
        Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show();
    }

    @Override
    public void clearMsg() {
        etUserName.setText("");
        etPassWord.setText("");
    }
}
